//
//  BKOrderWxManager.m
//  bestkeep
//
//  Created by 马乾征 on 16/12/6.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKODWXManager.h"

@implementation BKODWXObject

@end


@implementation BKODWXManager 

- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
        [self configWX];
    }
    return self;
}

+ (instancetype)manager
{
    static BKODWXManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[BKODWXManager alloc] initPrivate];
    });
    return manager;
}

- (void)configWX
{
    [WXApi registerApp:@"wx2c7367321c4ecab5"];
}

- (BOOL)isWXAppInstalled{
    return [WXApi isWXAppInstalled];
}

- (void)payRequestObject:(BKODWXObject *)obj
{
    if (!obj) {
        return;
    }
    
    PayReq *req = [[PayReq alloc] init];
    req.partnerId   = obj.partnerId;
    req.prepayId    = obj.prepayId;
    req.nonceStr    = obj.nonceStr;
    req.timeStamp   = obj.timeStamp;
    req.package     = obj.package;
    req.sign        = obj.sign;
    
    [WXApi sendReq:req];
}

- (void)handleOpenUrl:(NSURL *)url
{
    if ([[url scheme] isEqualToString:@"wx2c7367321c4ecab5"]) {
        [WXApi handleOpenURL:url delegate:self];
    }
}


//支付结果回调
- (void)onResp:(BaseResp *)resp
{
    if (![resp isKindOfClass:[PayResp class]]) {
        return;
    }
    
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODWXManagerDelegate)]) {
        PayResp *addCardResp = (PayResp *)resp;
        [self.delegate ODWXManagerDidRecvPayResponse:addCardResp];
    }
}

@end
